package org.zizi.session;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import org.zizi.manager.LoginManager;
import org.zizi.manager.Managers;
import org.zizi.util.Options;

public class SessionListener implements HttpSessionListener
{
	public static final String SESSION_TIMEOUT = "SessionTimeout";
	
	/** 创建会话时，设置过期时间 */
	public void sessionCreated(HttpSessionEvent event)
	{
		Integer timeout = Options.get().get(SESSION_TIMEOUT, Integer.class, null);
		if (timeout != null) event.getSession().setMaxInactiveInterval(timeout);
	}

	/** 销毁会话时，清空其识别信息 */
	public void sessionDestroyed(HttpSessionEvent event)
	{
		if (event == null) return;
		if (event.getSession() == null) return;
		
		LoginManager lm = Managers.get(LoginManager.class);
		if (lm == null) return;
		
		lm.remove(event.getSession().getId());
	}
}
